package com.kaikeba.controller;

import com.google.gson.Gson;
import com.kaikeba.bean.Message;
import com.kaikeba.dao.BaseADminDao;
import com.kaikeba.dao.impl.AdminDaoMysql;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.util.JsonUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;

/**
 * @Author: 平川
 * @Description:
 * @Date Created in 2021-07-29 15:01
 * @Modified By:
 */
public class AdminController {
    @ResponseBody("/admin/login.do")
    public String login(HttpServletRequest req, HttpServletResponse resp){
        //1.获取参数
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        //2.调取service层
        BaseADminDao adminDaoMysql = new AdminDaoMysql();
        boolean result = adminDaoMysql.login(username, password);
        //3.根据结果返回不同的数据
        Message message = null;
        if (result){
            message = new Message(0,"登录成功");
            //更新登陆时间，ip地址
            Date date = new Date();
            String remoteAddr = req.getRemoteAddr();
            adminDaoMysql.updateLoginTime(username,date,remoteAddr);
            req.getSession().setAttribute("adminUsername",username);
        }else{
            message = new Message(-1,"登录失败");
        }

        //4.JSON转化
        String json = JsonUtil.toJSON(message);
        //5.将数据放回给ajax
        return json;
    }
}
